<template>
	<view class="login-container">
		<!-- 背景图片 -->
		<view class="background-image"></view>
		
		<!-- 登录框 -->
		<view class="login-box">
			<!-- 标题 -->
			<view class="title">智慧养老系统</view>
			
			<!-- 表单 -->
			<view class="form">
				<!-- 账号输入框 -->
				<view class="input-group">
					<input 
						class="input" 
						type="text" 
						placeholder="请输入账号" 
						v-model="username"
						:focus="usernameFocus"
					/>
				</view>
				
				<!-- 密码输入框 -->
				<view class="input-group">
					<input 
						class="input" 
						:type="showPassword ? 'text' : 'password'" 
						placeholder="请输入密码" 
						v-model="password"
						:focus="passwordFocus"
					/>
					<view class="password-toggle" @click="togglePassword">
						<text class="toggle-text">{{ showPassword ? '隐藏' : '显示' }}</text>
					</view>
				</view>
				
				<!-- 记住密码 -->
				<view class="remember-section">
					<checkbox 
						:checked="rememberPassword" 
						@change="toggleRemember"
						color="#007AFF"
					/>
					<text class="remember-text">记住密码</text>
				</view>
				
				<!-- 登录按钮 -->
				<button 
					class="login-btn" 
					@click="handleLogin"
					:disabled="isLoading"
				>
					{{ isLoading ? '登录中...' : '登录' }}
				</button>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			username: '',
			password: '',
			rememberPassword: false,
			showPassword: false,
			usernameFocus: false,
			passwordFocus: false,
			isLoading: false
		}
	},
	onLoad() {
		// 页面加载时检查是否有保存的登录信息
		this.loadSavedCredentials();
	},
	methods: {
		// 切换密码显示/隐藏
		togglePassword() {
			this.showPassword = !this.showPassword;
		},
		
		// 切换记住密码
		toggleRemember(e) {
			this.rememberPassword = e.detail.value.length > 0;
		},
		
		// 加载保存的登录信息
		loadSavedCredentials() {
			const savedUsername = localStorage.getItem('saved_username');
			const savedPassword = localStorage.getItem('saved_password');
			const rememberStatus = localStorage.getItem('remember_password');
			
			if (rememberStatus && savedUsername && savedPassword) {
				this.username = savedUsername;
				this.password = savedPassword;
				this.rememberPassword = true;
			}
		},
		
		// 处理登录
		async handleLogin() {
			// 验证输入
			if (!this.username.trim()) {
				alert('请输入账号');
				return;
			}
			
			if (!this.password.trim()) {
				alert('请输入密码');
				return;
			}
			
			this.isLoading = true;
			
			try {
				// 这里可以调用登录API
				// const result = await this.loginAPI(this.username, this.password);
				
				// 模拟登录请求
				await new Promise(resolve => setTimeout(resolve, 1500));
				
				// 保存登录信息
				if (this.rememberPassword) {
					localStorage.setItem('saved_username', this.username);
					localStorage.setItem('saved_password', this.password);
					localStorage.setItem('remember_password', 'true');
				} else {
					localStorage.removeItem('saved_username');
					localStorage.removeItem('saved_password');
					localStorage.removeItem('remember_password');
				}
				
				// 保存登录状态
				localStorage.setItem('isLoggedIn', 'true');
				localStorage.setItem('currentUser', this.username);
				
				alert('登录成功');
				
				// 跳转到首页
				setTimeout(() => {
					this.$router.push('/index');
				}, 1500);
				
			} catch (error) {
				alert('登录失败，请检查账号密码');
			} finally {
				this.isLoading = false;
			}
		}
	}
}
</script>

<style scoped>
.login-container {
	position: relative;
	width: 100%;
	height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}

.background-image {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-image: url('../../img/background.png');
	background-size: cover;
	background-position: center;
	background-repeat: no-repeat;
	z-index: 1;
}

.login-box {
	position: relative;
	z-index: 2;
	width: 320px;
	background: rgba(255, 255, 255, 0.65);
	border-radius: 16px;
	padding: 40px 30px;
	box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
	backdrop-filter: blur(10px);
}

.title {
	font-size: 24px;
	font-weight: bold;
	color: #333;
	text-align: center;
	margin-bottom: 30px;
	letter-spacing: 1px;
}

.form {
	width: 100%;
}

.input-group {
	position: relative;
	margin-bottom: 20px;
}

.input {
	width: 100%;
	height: 48px;
	padding: 0 16px;
	border: 1px solid #e0e0e0;
	border-radius: 8px;
	font-size: 16px;
	color: #333;
	background: #fff;
	box-sizing: border-box;
	transition: border-color 0.3s ease;
}

.input:focus {
	border-color: #007AFF;
	outline: none;
}

.password-toggle {
	position: absolute;
	right: 12px;
	top: 50%;
	transform: translateY(-50%);
	padding: 4px 8px;
}

.toggle-text {
	font-size: 14px;
	color: #007AFF;
	cursor: pointer;
}

.remember-section {
	display: flex;
	align-items: center;
	margin-bottom: 30px;
}

.remember-text {
	margin-left: 8px;
	font-size: 14px;
	color: #666;
}

.login-btn {
	width: 100%;
	height: 48px;
	background: #007AFF;
	color: #fff;
	border: none;
	border-radius: 8px;
	font-size: 16px;
	font-weight: 500;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background-color 0.3s ease;
}

.login-btn:active {
	background: #0056CC;
}

.login-btn:disabled {
	background: #ccc;
	color: #999;
}

/* 响应式设计 */
@media screen and (max-width: 480px) {
	.login-box {
		width: 280px;
		padding: 30px 20px;
		margin: 0 20px;
	}
	
	.title {
		font-size: 20px;
		margin-bottom: 25px;
	}
	
	.input {
		height: 44px;
		font-size: 15px;
	}
	
	.login-btn {
		height: 44px;
		font-size: 15px;
	}
}
</style>
